package cn.lingyangwl.agile.pay.common.manager.impl;

import cn.lingyangwl.agile.pay.common.constants.MqCons;
import cn.lingyangwl.agile.pay.common.manager.PayMqSender;
import cn.lingyangwl.agile.pay.common.model.mq.MchNotifyMQ;
import cn.lingyangwl.agile.pay.common.model.mq.RefundOrderMQ;
import cn.lingyangwl.agile.pay.common.model.mq.PayOrderCloseMQ;
import cn.lingyangwl.agile.pay.common.model.mq.ResetIsvMchAppInfoConfigMQ;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Service
public class PayMqSenderImpl implements PayMqSender {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public void send(PayOrderCloseMQ data) {
        rabbitTemplate.convertAndSend(MqCons.DELAYED_EVENT_EXCHANGE, PayOrderCloseMQ.DELAY_ROUTING_KEY, data.toMessage(), correlationData -> {
            correlationData.getMessageProperties().setDelay(PayOrderCloseMQ.TTL);
            return correlationData;
        });
    }

    @Override
    public void send(RefundOrderMQ data) {
        rabbitTemplate.convertAndSend(MqCons.DELAYED_EVENT_EXCHANGE, RefundOrderMQ.DELAY_ROUTING_KEY, data.toMessage(), correlationData -> {
            correlationData.getMessageProperties().setDelay(RefundOrderMQ.TTL);
            return correlationData;
        });
    }

    @Override
    public void send(ResetIsvMchAppInfoConfigMQ data) {
        rabbitTemplate.convertAndSend(MqCons.EVENT_EXCHANGE, "", data.toMessage());
    }

    @Override
    public void sendDelay(MchNotifyMQ data, int delay) {
        rabbitTemplate.convertAndSend(MqCons.DELAYED_EVENT_EXCHANGE, MchNotifyMQ.ROUTE_KEY, data.toMessage(), correlationData -> {
            correlationData.getMessageProperties().setDelay(delay * 1000);
            return correlationData;
        });
    }
}
